package com.ruoyi.web.controller.files.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class PdfUtils {

    /**
     * 截取pdfFile的第from页至第end页，组成一个新的文件名
     *
     * @param pdfFile 要切割的pdf文件
     * @param newFile 切割后形成的新的pdf文件
     * @param from    从第N页开始
     * @param end     到第N页结束
     */
    public static void partitionPdf(String pdfFile, String newFile, int from, int end) {
        Document document = null;
        PdfCopy copy = null;
        PdfReader reader = null;
        try {
            reader = new PdfReader(pdfFile);
            int pageCount = reader.getNumberOfPages();
            if (from < 1) {
                from = 1;
            }
            if (from > pageCount) {
                from = pageCount;
            }
            if (end == 0 || end > pageCount) {
                end = pageCount;
            }
            document = new Document(reader.getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(newFile));
            document.open();
            for (int j = from; j <= end; j++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, j);
                copy.addPage(page);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                document.close();
            }
            if (copy != null) {
                copy.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }

    /**
     * pdf转图片
     *
     * @param pdfFile   PDF 文件
     * @param imageFile 输出的图片文件
     * @param from      开始页 从1开始
     * @param end       结束页 最大为PDF总页数
     * @throws Exception
     */
    public static void pdfToImage(String pdfFile, String imageFile, int from, int end) throws Exception {
        PDDocument doc = null;
        ByteArrayOutputStream os = null;
        InputStream stream = null;
        OutputStream out = null;
        try {
            //pdf路径
            stream = new FileInputStream(pdfFile);
            // 加载解析PDF文件
            doc = PDDocument.load(stream);
            PDFRenderer pdfRenderer = new PDFRenderer(doc);
            PDPageTree pages = doc.getPages();
            int pageCount = pages.getCount();
            if (from < 1) {
                from = 1;
            }
            if (from > pageCount) {
                from = pageCount;
            }
            if (end == 0 || end > pageCount) {
                end = pageCount;
            }
            for (int i = from; i <= end; i++) {
                //PDFBOX 是从0开始的，from初始值为1，所以这边要减 i-1
                BufferedImage bim = pdfRenderer.renderImageWithDPI(i - 1, 200);
                os = new ByteArrayOutputStream();
                ImageIO.write(bim, "jpg", os);
                byte[] dataList = os.toByteArray();
                // jpg文件转出路径
                File file = new File(imageFile.replace(".jpg", i + ".jpg"));
                if (!file.getParentFile().exists()) {
                    // 不存在则创建父目录及子文件
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }
                out = new FileOutputStream(file);
                out.write(dataList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (doc != null) {
                doc.close();
            }
            if (os != null) {
                os.close();
            }
            if (stream != null) {
                stream.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    public static void pdfGetCoverImage(String pdfFile, String imageFile) throws Exception {
        pdfToImage(pdfFile,imageFile,1,1);
    }
}